package tt.dz.egobus.servicemanager.controller.runtimeconfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tt.dz.egobus.api.ResponseBack;
import tt.dz.egobus.api.commoncenter.bean.ConfigBack;
import tt.dz.egobus.api.commoncenter.bean.ConfigTypeBack;
import tt.dz.egobus.servicemanager.base.controller.CommonBinderController;
import tt.dz.egobus.servicemanager.core.exception.BusinessException;
import tt.dz.egobus.servicemanager.service.SystemConfigManagerService;
import tt.dz.egobus.servicemanager.util.PojoConvertVoUtils;
import tt.dz.egobus.servicemanager.vo.JsonBack;
import tt.dz.egobus.servicemanager.vo.ModuleViewVo;
import tt.dz.egobus.servicemanager.vo.RuntimeConfigViewVo;

import java.util.List;

import static tt.dz.egobus.servicemanager.bean.ServiceManagerCodeStatus.SYSTEM_CODE_ERROR;

/**
 * @author Anson
 * @date 17-8-11
 * @name 运行中的系统配置
 * @since 1.0.6
 */
@RestController
@RequestMapping("/runtimeconfig/system")
public class SystemConfigController extends CommonBinderController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SystemConfigController.class);

    @Autowired
    @Qualifier("dubboSystemConfigServiceImpl")
    private SystemConfigManagerService service;

    /**
     * @author Anson
     * @date 17-8-11
     * @name 查询所有模块集合
     * @see tt.dz.egobus.servicemanager.facade.common.CommonFacade
     * @since 1.0.6
     */
    @RequestMapping(value = "/modules", method = RequestMethod.POST)
    public JsonBack<List<ModuleViewVo>> modules() {
        JsonBack<List<ModuleViewVo>> back = null;
        try {
            ResponseBack<List<ConfigTypeBack>> responseBack = service.getAllConfigTypeBack();
            if (responseBack.isSuccess()) {
                List<ConfigTypeBack> modules = responseBack.getResult();
                List<ModuleViewVo> datas = PojoConvertVoUtils.convertModuleViewVoList(modules);
                back = JsonBack.success(datas);
            } else {
                back = JsonBack.instance(responseBack, null);
            }
        } catch (BusinessException e) {
            back = JsonBack.error(e);
            LOGGER.error("模块查询", e);
        } catch (Exception e) {
            back = JsonBack.error(SYSTEM_CODE_ERROR);
            LOGGER.error("模块查询", e);
        }
        return back;
    }

    @RequestMapping(value = "/module", method = RequestMethod.POST)
    public JsonBack<List<RuntimeConfigViewVo>> module(@RequestParam("moduleCode") int moduleCode) {
        JsonBack<List<RuntimeConfigViewVo>> back = null;
        try {
            ResponseBack<List<ConfigBack>> responseBack = service.getSystemConfig(moduleCode);
            if (responseBack.isSuccess()) {
                List<ConfigBack> configs = responseBack.getResult();
                List<RuntimeConfigViewVo> datas = PojoConvertVoUtils.convertRuntimeConfig(configs);
                back = JsonBack.success(datas);
            } else {
                back = JsonBack.instance(responseBack, null);
            }
        } catch (BusinessException e) {
            back = JsonBack.error(e);
            LOGGER.error("模块查询配置", e);
        } catch (Exception e) {
            back = JsonBack.error(SYSTEM_CODE_ERROR);
            LOGGER.error("模块查询配置", e);
        }
        return back;
    }

    /**
     * @author Anson
     * @date 17-4-18
     * @name 更新
     * @since 1.0.4
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonBack<RuntimeConfigViewVo> update(@RequestParam("moduleCode") int moduleCode,
                                                @RequestParam("key") String key,
                                                @RequestParam("value") String value) {
        JsonBack<RuntimeConfigViewVo> back = null;
        try {
            ResponseBack<ConfigBack> responseBack =
                    service.updateConfigValue(moduleCode, key, value);
            if (responseBack.isSuccess()) {
                RuntimeConfigViewVo vo = new RuntimeConfigViewVo();
                ConfigBack result1 = responseBack.getResult();
                back = JsonBack.success(result1 == null ? vo : vo.view(result1));
            } else {
                back = JsonBack.instance(responseBack, null);
            }
        } catch (BusinessException e) {
            back = JsonBack.error(e);
            LOGGER.error("修改配置", e);
        } catch (Exception e) {
            back = JsonBack.error(SYSTEM_CODE_ERROR);
            LOGGER.error("修改配置", e);
        }
        return back;
    }

}
